#include "myhead.h"

//线程1--实时显示音量进度条
void *Pthread_Show_Volume(void *arg)
{
    char buf[50];

    while(1)
    {
        memset(buf,0,50);
        sprintf(buf,"volume %d 1\n",music_volume);

        pthread_mutex_lock(&mutex);//上锁
        //如果暂停了或者静音了
        if((mplayer_stop == 1) || (mplayer_mute == 1))
        {
            pthread_mutex_unlock(&mutex);//解锁
            while((mplayer_stop == 1) || (mplayer_mute == 1));
        }
        else
        {
            Send_Command(buf);
            pthread_mutex_unlock(&mutex);//解锁
        }

        //显示当前音量条
        Lcd_Show_Color(70,445,music_volume,10,0xADD8E6);
        usleep(100000);
        //显示数字
        memset(buf,0,50);
        sprintf(buf,"%d%%",music_volume);
        Display_characterX(180,442,buf,0xADD8E6,1);

        usleep(100000);
        //检查是否有取消请求
        pthread_testcancel();
    }
    pthread_exit(NULL);
}

//线程2--实时拖动音量进度条
void *Pthread_Drag_Volume(void *arg)
{
    int x = 50;
    int y = 10;
    char buf[50];
    int i = 0;
    int current_volume = 0;

    while(1)
    {
        if(mplayer_stop == 0)
        {
            memset(buf,0,50);
            sprintf(buf,"volume %d 1\n",mplayer_mute);
            pthread_mutex_lock(&mutex);//上锁
            Send_Command(buf);
            pthread_mutex_unlock(&mutex);//解锁
        }

        i = 0;
        //读取触摸屏的信息
        read(ts_fd2,&ts_event2,sizeof(struct input_event));
        //按下
        if(ts_event2.type == 1 && ts_event2.code == 330 && ts_event2.value == 1)
        {
            while(1)
            {
                read(ts_fd2,&ts_event2,sizeof(struct input_event));
                if(ts_event2.type == EV_ABS  && ts_event2.code == ABS_X)
                {
                    x = ts_event2.value*(800.0/1024.0);
                }
                if(ts_event2.type == EV_ABS  && ts_event2.code == ABS_Y)
                {
                    y = ts_event2.value*(480.0/600.0);
                    if(x>50 && x<190 && y> 420 && y<480)
                    {
                        printf("volume:x2 = %d,y2 = %d\n",x,y);
                        //如果坐标大于有效范围，则退出循环
                        if(x>170)
                        {
                            break;
                        }

                        current_volume = (x - 70) * 100 / (170 - 70); // 根据触摸位置计算音量值
                        if(current_volume < 0)
                        {
                            current_volume = 0;
                        }
                        else if(current_volume > 100)
                        {
                            current_volume = 100;
                        }
                        music_volume = current_volume; // 更新音量值

                        ShowJpg(map , "pic/music/volume_ground.jpg" , 60, 420 , 1 , 1);
                        //显示声音进度条
                        Lcd_Show_Color(70,445,current_volume,10,0xADD8E6);
                        usleep(100000);

                        //显示数字
                        //背景图片
                        memset(buf,0,50);
                        sprintf(buf,"%d%%",current_volume);
                        Display_characterX(180,442,buf,0xADD8E6,1);
                        usleep(100000);

                        sprintf(buf,"volume %d 1\n", current_volume); // 更新命令字符串中的音量值
                        pthread_mutex_lock(&mutex);//上锁
                        Send_Command(buf);
                        pthread_mutex_unlock(&mutex);//解锁
                    }
                }
            }
        }
        // 检查是否有取消请求
        pthread_testcancel();
    }
    pthread_exit(NULL);
}

// 线程3--实时显示进度条
void *Pthread_Show_ProGress_Bar(void *arg)
{
    char len[1024] = {0};   // 存储读取的字符串
    int i;                  // 进度值

    while(1)
    {
        if(mplayer == 1)    // 如果播放器启动了
        {   
            pthread_mutex_lock(&mutex);    // 上锁

            if(mplayer_stop == 1)   // 如果停了
            {
                pthread_mutex_unlock(&mutex);      // 解锁
                while(mplayer_stop == 1);           // 等待重新播放
                pthread_mutex_lock(&mutex);        // 上锁
            }

            time_length = 0;
            Send_Command("get_time_length\n");     // 发送获取总播放时间的指令
           
            if(mplayer_mute == 1)
            {
                Send_Command("pause\n");            // 静音暂停
            }

            while(1)
            {
                memset(len, 0, 1024);
                fgets(len, 1024, fp_mplayer);
                sscanf(len, "ANS_LENGTH=%d", &time_length);    // 解析获取到的总播放时间
                if(time_length != 0)
                {
                    break;
                }
            }
            pthread_mutex_unlock(&mutex);      // 解锁

            pthread_mutex_lock(&mutex);        // 上锁

            if(mplayer_stop == 1)
            {
                pthread_mutex_unlock(&mutex);       // 解锁
                while(mplayer_stop == 1);            // 等待重新播放
                pthread_mutex_lock(&mutex);         // 上锁
            }

            Send_Command("get_time_pos\n");         // 发送获取当前播放时间的指令

            if(mplayer_mute == 1)
            {
                Send_Command("pause\n");             // 静音暂停
            }

            while(1)
            {
                memset(len,0,1024);
                fgets(len,1024,fp_mplayer);
                sscanf(len, "ANS_TIME_POSITION=%d\n", &time_pos);    // 解析获取到的当前播放时间
                if(time_length != 1000)     // 如果解析成功
                {
                    break;
                }
            }
            pthread_mutex_unlock(&mutex);       // 解锁

            pthread_mutex_lock(&mutex);         // 上锁

            if(mplayer_stop == 1)
            {
                pthread_mutex_unlock(&mutex);       // 解锁
                while(mplayer_stop == 1);            // 等待重新播放
                pthread_mutex_lock(&mutex);         // 上锁
            }

            i = 1000;
            Send_Command("get_percent_pos\n");      // 发送获取进度百分比的指令

            if(mplayer_mute == 1)
            {
                Send_Command("pause\n");             // 静音暂停
            }

            while(1)
            {
                memset(len,0,1024);
                fgets(len,1024,fp_mplayer);
                sscanf(len , "ANS_PERCENT_POSITION=%d\n", &i);    // 解析获取到的进度值
                if(i != 1000)       // 如果解析成功
                {
                    break;
                }
            }
            pthread_mutex_unlock(&mutex);       // 解锁

            ShowJpg(map , "pic/music/jdt_time.jpg" , 530, 400 , 1 , 1);   // 显示图片

            // 当前时长
            memset(len,0,1024);
            sprintf(len,"%d:%d", time_pos / 60, time_pos % 60);
            Display_characterX(542, 405, len, 0xADD8E6, 1);

            // 斜杠
            memset(len,0,1024);
            sprintf(len,"/");
            Display_characterX(580, 404, len, 0xADD8E6, 1);

            // 总时长
            memset(len,0,1024);
            sprintf(len,"%d:%d",time_length/60,time_length%60);
            Display_characterX(597,405,len,0xADD8E6,1);


            // 进度条
            Lcd_Show_Color(80, 405, 450, 10, 0x00000000);
            Lcd_Show_Color(80, 405, 4.5 * i, 10, 0xADD8E6);

            usleep(100000);    // 线程暂停一段时间，避免频繁更新进度条

        }
        
        pthread_testcancel();   // 检查是否有取消请求
    }

    pthread_exit(NULL);
}

//线程4--实时拖动进度条
void *Pthread_Drag_ProGress_Bar(void *arg)
{
    usleep(5000);
    int x1 = 0;
    int y1 = 0;
    int x2 = 0;
    int y2 = 0;
    char buf[50];
    int i = 0;

    while(1)
    {
        //读取触摸屏的信息
        read(ts_fd3,&ts_event3,sizeof(struct input_event));
        if(ts_event3.type == 1 && ts_event3.code == 330 && ts_event3.value == 1)
        {
            while(1)
            {
                read(ts_fd3,&ts_event3,sizeof(struct input_event));
                if(ts_event3.type == EV_ABS  && ts_event3.code == ABS_X)
                {
                    x1 = ts_event3.value*(800.0/1024.0);
                }
                if(ts_event3.type == EV_ABS  && ts_event3.code == ABS_Y)
                {   
                    y1 = ts_event3.value*(480.0/600.0);
                    if((y1 <= 480 && y1 >= 400) && (x1 >= 0 && x1 <= 800) ||  (x1 >= 650 && x1 <= 800))
                    {
                        i = 1;
                    }
                    break;
                }
            }

            if(i == 1)
            {
                i = 0;
                continue;
            }

            pthread_mutex_lock(&mutex);//上锁
            while(1)
            {
                read(ts_fd3,&ts_event3,sizeof(struct input_event));
                if(ts_event3.type == EV_ABS  && ts_event3.code == ABS_X)
                {
                    x2 = ts_event3.value*(800.0/1024.0);
                }
                if(ts_event3.type == EV_ABS  && ts_event3.code == ABS_Y)
                {
                    y2 = ts_event3.value*(480.0/600.0);

                   if(y2 - 390 > 30 || x2 > 700 || x2 < 100)
                    {
                        Send_Command(buf);
                        mplayer_stop = 0;
                        pthread_mutex_unlock(&mutex);//解锁	
                        break;
                    }
                    
                    memset(buf,0,50);
                    if(x2 > x1)
                    {
                        sprintf(buf,"seek +%d\n",abs((((x2-120)*(int)time_length)/570)-(int)time_pos));
                    }
                    else
                    {
                        music_move = 1;
                        sprintf(buf,"seek -%d\n",abs((((x2-120)*(int)time_length)/570)-(int)time_pos));
                    }

                }
                //滑动结束
                if(ts_event3.type == 1 && ts_event3.code == 330 && ts_event3.value == 0)
                {
                    if(y2 != 0 && x2 != 0)
                    {
                        Send_Command(buf);
                        mplayer_stop = 0;
                        pthread_mutex_unlock(&mutex);//解锁	
                    break;
                    }
                }
            }
        }
        x2 = 0, y2 = 0;
        // 检查是否有取消请求
        pthread_testcancel();
    }

    pthread_exit(NULL);
}

